package com.deng.codetemplate.config.aop;

import com.deng.codetemplate.config.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author 邓天天
 * @date 2021/4/26 21:54
 */
@Aspect
@Component
public class GlobalAspect {

    @Around("execution(public * com.deng.codetemplate.controller.*.*(..))")
    public Result<Object> around(ProceedingJoinPoint joinPoint) {
        try {
            long currentTimeMillis = System.currentTimeMillis();
            Result<Object> result = (Result<Object>) joinPoint.proceed();
            result.setExecutionTime(System.currentTimeMillis() - currentTimeMillis);
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return Result.error().errorMsg("出现了异常");
        }
    }
}
